/*
 * SearchWindow.java
 *
 * Created on __DATE__, __TIME__
 */

package gui;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import core.Core;
import core.UserEntry;

/**
 * 
 * @author __USER__
 */
public class SearchWindow extends javax.swing.JDialog {

	private Core core = null;
	private DefaultTableModel model = null;

	/** Creates new form SearchWindow */
	public SearchWindow(java.awt.Frame parent, boolean modal) {
		super(parent, modal);

		core = Core.get();
		initComponents();
		initTable();
	}

	public void initTable() {
		List<UserEntry> entries = new ArrayList<UserEntry>();

		model = (DefaultTableModel) jTable1.getModel();
		model.setNumRows(0);
		model.setColumnCount(0);
		jTable1.setColumnSelectionAllowed(false);
		jTable1.getTableHeader().setReorderingAllowed(false);

		model.addColumn("Date");
		model.addColumn("Category");
		model.addColumn("Comment");
		model.addColumn("Debit");
		model.addColumn("Credit");
		jTable1.getColumnModel().getColumn(3)
				.setCellRenderer(new CellRendererRed());
		jTable1.getColumnModel().getColumn(4)
				.setCellRenderer(new CellRendererGreen());
		for (int i = 0; i < 8; i++)
			model.addRow(new Object[] {});
	}

	private void FillTable(List<UserEntry> entries) {
		System.out.println(entries.size() + " element(s) founded.");
		model.setNumRows(0);

		DecimalFormat df = new DecimalFormat("0.00\u20ac");
		Iterator<UserEntry> i = entries.iterator();
		while (i.hasNext()) {
			System.out.println("add de l'element");
			UserEntry current = i.next();
			SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
			String catName = core.getCategoryName(current.getCategoryId());
			if (catName == null)
				catName = new String("None");
			if (!current.isCredit()) {
				model.addRow(new Object[] { format.format(current.getDate()),
						catName, current.getComment(), null,
						df.format(current.getValue()) });
			} else {
				model.addRow(new Object[] { format.format(current.getDate()),
						catName, current.getComment(),
						df.format(Math.abs(current.getValue())), null });
			}
		}
		for (int a = 0; a < 8; a++)
			model.addRow(new Object[] {});
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		closeButton = new javax.swing.JButton();
		jPanel2 = new javax.swing.JPanel();
		jTextField1 = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Search");

		jPanel1.setBackground(new java.awt.Color(141, 178, 227));

		jTable1.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null } }, new String[] { "Title 1",
						"Title 2", "Title 3", "Title 4" }));
		jScrollPane1.setViewportView(jTable1);

		closeButton.setText("Close");
		closeButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				closeSearchWindow(evt);
			}
		});

		jPanel2.setOpaque(false);

		jTextField1.setHorizontalAlignment(javax.swing.JTextField.LEFT);

		jButton1.setText("OK");
		jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				callSearch(evt);
			}
		});

		jLabel1.setText("Search");

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout
				.setHorizontalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(jLabel1)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												72, Short.MAX_VALUE)
										.addComponent(
												jTextField1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												328,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(jButton1)
										.addContainerGap()));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(jButton1)
														.addComponent(
																jTextField1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						jPanel1Layout
								.createSequentialGroup()
								.addComponent(jPanel2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(187, Short.MAX_VALUE))
				.addComponent(jScrollPane1,
						javax.swing.GroupLayout.DEFAULT_SIZE, 707,
						Short.MAX_VALUE)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						jPanel1Layout.createSequentialGroup()
								.addContainerGap(610, Short.MAX_VALUE)
								.addComponent(closeButton).addGap(36, 36, 36)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addComponent(
												jPanel2,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												146,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												26, Short.MAX_VALUE)
										.addComponent(closeButton)
										.addContainerGap()));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

		pack();
	}// </editor-fold>
		// GEN-END:initComponents

	private void callSearch(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		if (!jTextField1.getText().isEmpty()) {
			List<UserEntry> entries = Core.get().getEntriesSearch(
					jTextField1.getText());
			FillTable(entries);
		}
	}

	private void closeSearchWindow(java.awt.event.MouseEvent evt) {
		this.dispose();
		// if (!jTextField1.getText().isEmpty()){
		// List<UserEntry> entries =
		// Core.get().getEntriesSearch(jTextField1.getText());
		// FillTable(entries);
		// }
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				SearchWindow dialog = new SearchWindow(
						new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton closeButton;
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private javax.swing.JTextField jTextField1;
	// End of variables declaration//GEN-END:variables
}